﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using ZZQFrameWork;

/// <summary>
/// 技能面板技能信息界面
/// </summary>
public class SkillPanelSkillInfo:MonoBehaviour
{
    //private SkillModule skillModule
    //{
    //    get { return ModuleManager.Instance.Get<SkillModule>(); }
    //}
    private CharacterModule characterModule
    {
        get { return ModuleManager.Instance.Get<CharacterModule>(); }
    }

    private UIAtlas skillAtlas;

    public UIAtlas SkillAtlas
    {
        get
        {
            if (skillAtlas == null)
            {
                skillAtlas = UIAtlasManager.Instance.GetUIAtlasByName("Skill_Atlas");
            }
            return skillAtlas;
        }
    }

    /// <summary>
    /// 技能排序Grid
    /// </summary>
    private GridLayoutGroup skillGrid;

    /// <summary>
    /// 技能item预设
    /// </summary>
    private GameObject skillItemPrefab;

    /// <summary>
    /// 技能信息集合
    /// </summary>
    private List<SkillVO> skillList=new List<SkillVO>();

    /// <summary>
    /// 技能item集合
    /// </summary>
    private List<SkillPanelSkillItem> skillItemList=new List<SkillPanelSkillItem>(); 

    public GameObject SkillItemPrefab
    {
        get {
            if (skillItemPrefab == null)
            {
                skillItemPrefab = ResManager.Instance.Load("SkillItem") as GameObject;
            }
            return skillItemPrefab;
        }
    }
    
    /// <summary>
    /// 当前点击的技能item
    /// </summary>
    private SkillPanelSkillItem currentSkillItem;

    //private List<> 

    void Awake()
    {
        skillGrid = transform.GetComponentByPath<GridLayoutGroup>("SkillItemGrid");
        for (int i = 0; i < 22; i++)
        {
            //int id = SkillPanelDefcultSkill.getSkillId(i);
            int id = int.Parse(SkillPanelDefcultSkillConfigProvider.Instance.GetSkillById(i+1).skillId);
            SkillVO skill = new SkillVO(id);
            skillList.Add(skill);
            skillItemList.Add(CreatSkillItem(skill));
        }
        for (int i = 0; i < skillItemList.Count; i++)
        {
            EventTriggerListener.Get(skillItemList[i].gameObject).SetEventHandle(Enum_TouchEventType.OnClick, OnSkillItemClick);
        }
    }

    void OnEnable()
    {
        RefreshPanel();
    }

    /// <summary>
    /// 创建技能Item
    /// </summary>
    /// <param name="skill"></param>
    /// <returns></returns>
    private SkillPanelSkillItem CreatSkillItem(SkillVO skill)
    {
        GameObject go = Instantiate(SkillItemPrefab);
        go.transform.SetParent(skillGrid.transform,false);
        go.name = "SkillItem" + skill.id;
        SkillPanelSkillItem item = go.GetOrAddComponent<SkillPanelSkillItem>();

        item.SetSkillInfo(this);
        item.Data = skill;
        return item;
    }

    /// <summary>
    /// 技能item点击
    /// </summary>
    /// <param name="_listener"></param>
    /// <param name="_args"></param>
    /// <param name="_params"></param>
    private void OnSkillItemClick(GameObject _listener, object _args, object[] _params)
    {
        SkillPanelSkillItem item = _listener.GetComponent<SkillPanelSkillItem>();
        if (currentSkillItem != null)
        {
            if (currentSkillItem == item)
            {
                Debug.Log("点击的技能一样，开始尝试学习");
                if (!item.IsLocked&&!item.IsLearn)
                {
                    Debug.Log("该技能已经解锁也没有学习，开始查看是否有足够的属性点");
                    //学习该技能
                    if (ModuleManager.Instance.Get<CharacterModule>().LearnSkill(item.Data))
                    {
                        Debug.Log("学习技能成功！");
                        item.IsLearn = true;
                    }
                    else
                    {
                        Debug.Log("学习技能失败！");
                    }
                }
                return;
            }
            currentSkillItem.OnItemClicked(false);
        }
        currentSkillItem = item;
        currentSkillItem.OnItemClicked(true);
        EventDispatcher.TriggerEvent(Events.CharacterEvent.OnSkillPanelTipsShow,item.Data);
    }

    /// <summary>
    /// 刷新界面
    /// </summary>
    void RefreshPanel()
    {
        List<SkillVO> list = characterModule.mainRole.skillList;
        for (int i = 0; i < skillItemList.Count; i++)
        {
            if (skillItemList[i].Data.lvNeed <= characterModule.mainRole.lv)
            {
                skillItemList[i].IsLocked = false;
            }
        }
        for (int i = 0; i < list.Count; i++)
        {
            SkillPanelSkillItem item = skillItemList.Find((obj) =>
            {
                return obj.Data.id == list[i].id;
            }); 
            item.IsLearn = true;
        }
    }
}

